<?php
/**
 * 
 * @author yuxun
 *
 */

class ModMessage extends Model {

    protected $table = 'message';

    // 表名
    public function tableName(){
        return $this->table;
    }

    public static function model($className=__CLASS__){
        return parent::model($className);
    }

    public function getDbConnection(){
        return Yii::app()->dbOss;
    }

    public function addmuch($params){
    	$result = $this->getDbConnection()->multiInsert($params, $this->table, FALSE);
    	return $result;
    }
    
    public function select($params){
    	$table = $this->table;
    	$where  = ' WHERE game=:game ';
    	$args   = array(
    			':game'     => $params['g']
    	);

    	if($params['os'] != '' && $params['os'] != 'ALL'){
    		//先根据os查询渠道id
    		$modChannel = new ModChannel();
    		$channelObj = $modChannel->findByAttributes(array('game'=>$params['g'],'tab'=>$params['os']));
    		$channel_id = $channelObj->id;
    		$where .= " AND channel_id = :channel_id ";
    		$args['channel_id'] = $channel_id;
    	}

        if(!empty($params['server_id'])){
    		$where .= " AND server_id = :server_id ";
    		$args['server_id'] = $params['server_id'];
    	}  
    	
    	if(!empty($params['role_id'])){
    		$where .= " AND role_id like :role_id ";
    		$args['role_id'] = '%'.$params['role_id'].'%';
    	}
    	
    	if(!empty($params['role_name'])){
    		$where .= " AND role_name like :role_name ";
    		$args['role_name'] = '%'.$params['role_name'].'%';
    	}
    	
    	if(!empty($params['role_level'])){
    		$where .= " AND role_level = :role_level ";
    		$args['role_level'] = $params['role_level'];
    	}

    	$fields = "*";
    	$sql = "SELECT {$fields} FROM {$table} {$where}";
    	$data = $this->getDbConnection()->query($sql, $args);    	
    	return $data;
    }
    
    public function page($params){
    	$table = $this->table;
    	$where  = ' WHERE game=:game ';
    	$args   = array(
    			':game'     => $params['g']
    	);
    	
        if($params['os'] != '' && $params['os'] != 'ALL'){
        	//先根据os查询渠道id
        	$modChannel = new ModChannel();
        	$channelObj = $modChannel->findByAttributes(array('game'=>$params['g'],'tab'=>$params['os']));
        	$channel_id = $channelObj->id;
    		$where .= " AND channel_id = :channel_id ";
    		$args['channel_id'] = $channel_id;
    	}   

        if(!empty($params['server'])){
    		$where .= " AND server_id = :server_id ";
    		$args['server_id'] = $params['server'];
    	}  
    	
    	if(!empty($params['role_id'])){
    		$where .= " AND role_id like :role_id ";
    		$args['role_id'] = '%'.$params['role_id'].'%';
    	}
    	
    	if(!empty($params['role_name'])){
    		$where .= " AND role_name like :role_name ";
    		$args['role_name'] = '%'.$params['role_name'].'%';
    	}
    	
    	if(!empty($params['role_level'])){
    		$where .= " AND role_level = :role_level ";
    		$args['role_level'] = $params['role_level'];
    	}
    	
    	if($params['begin'] && $params['end']){
    		$begin = strtotime($params['begin'] . ' 00:00:00');
    		$end = strtotime($params['end'] . ' 23:59:59');
    		$where  .= " AND create_time BETWEEN ".$begin. " AND  ".$end;
    	}
    	 
    	if($params['begin'] != '' && $params['end'] == ""){
    		$begin = strtotime($params['begin'] . ' 00:00:00');
    		$end = time();
    		$where  .= " AND create_time BETWEEN ".$begin. " AND  ".$end;
    	}
    	 
    	if($params['begin'] == '' && $params['end'] != ""){
    		$params['begin'] = date('Y-m-01');
    		$begin = strtotime($params['begin'] . ' 00:00:00');
    		$end = strtotime($params['end'] . ' 23:59:59');
    		$where  .= " AND create_time BETWEEN ".$begin. " AND  ".$end;
    	}
    	
    	if(!empty($params['desc'])){
    		$where .= " AND `desc` like :desc ";
    		$args['desc'] = '%'.$params['desc'].'%';
    	}
    	
    	$c_sql = "select count(1) as cnt from {$table} {$where}";
    	$c_result = $this->getDbConnection()->query($c_sql, $args);
    	$count = isset($c_result[0]['cnt']) ? $c_result[0]['cnt'] : 0;

    	$order  = self::getOrder($params['order']);
    	$limit  = PubFunc::getLimit($params['length'], $params['page']);
    	$fields = "*";
    	$sql = "SELECT {$fields} FROM {$table} {$where} {$order} {$limit}";

    	$data = $this->getDbConnection()->query($sql, $args);
    	$result = array('count'=>$count,'data'=>$data);
    	return $result;
    }
    
    public static function getOrder($order){
    	if(PubFunc::checkEmpty($order, array(0, 1))) return '';
    	if(!in_array($order[1], array('asc', 'desc'))){
    		$sort = 'asc';
    	}else{
    		$sort = $order[1];
    	}
    	return " ORDER BY {$order[0]} {$sort} ";
    }
    
    /**
     * 根据意见反馈$message_id查询回复信息
     * @param unknown $message_id
     * @return multitype:unknown
     */
    public function replyView($message_id){
    	$sql = "SELECT mr.*,sp.title,sp.content,sp.item,sp.status from message_reply as mr INNER JOIN send_person as sp on sp.id = mr.send_person_id WHERE message_id = {$message_id}";
    	$result = $this->getDbConnection()->query($sql, $args);	
    	return $result;
    }
    
    
} 